import './public-path'
import Vue from 'vue'
import App from './App.vue'
import routes from './router/index'
import store from './store'
import ElementUI from 'element-ui'
import common from './qiankun-common'
import VuePlugin from 'vue-plugin'
import VueRouter from 'vue-router'
import 'element-ui/lib/theme-chalk/index.css'
import 'vue-plugin/lib/static/css/theme-red/index.css'

// 接口服务
import serviceMap from './serviceMap.js'

Vue.use(VueRouter)
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error)
}

Vue.config.productionTip = false
Vue.prototype.$showNavBar = true

Vue.use(ElementUI, {
  size: 'medium'
})

//微应用配置
let instance = null
let router = null

function creatVueRouter() {
  return new VueRouter({
    mode: 'history',
    base: process.env.VUE_APP_PUBLIC_PATH,
    routes
  })
}
router = creatVueRouter()

Vue.use(VuePlugin, {
  store,
  router,
  config: {
    debug: 'VUE_APP_BASE_API',
    processEnv: process.env,
    project: {
      moduleName: '智能客服系统', //项目名
      type: 'system', //账号体系,  运营体系：system; 分支机构体系：company
      service: 'VUE_APP_IMP_ACCOUNT', //接口服务
      jumpLink: '/customerService/customerseSrviceModuleManagement',
      systemKey: 'CUSTOMERSERVICESYSTEM'
    },
    serviceMap
  }
})
// power.powerMixin(Vue); //全局注入权限组件选项
function render(props = {}) {
  const { container } = props
  if (window.__POWERED_BY_QIANKUN__ && window.__CACHE_INSTANCE_BY_QIAN_KUN_FOR_VUE__) {
    const cachedInstance = window.__CACHE_INSTANCE_BY_QIAN_KUN_FOR_VUE__
    router = creatVueRouter()
    // 从最初的Vue实例上获得_vnode
    const cachedNode = cachedInstance._vnode

    // 让当前路由在最初的Vue实例上可用
    router.apps.push(...cachedInstance.catchRoute.apps)

    instance = new Vue({
      router,
      store,
      render: () => cachedNode
    })

    // 缓存最初的Vue实例
    instance.cachedInstance = cachedInstance

    instance.$mount(container ? container.querySelector('#app') : '#app')
  } else {
    instance = new Vue({
      router,
      store,
      render: (h) => h(App)
    }).$mount(container ? container.querySelector('#app') : '#app')
  }
  window.$vm = instance

  // vue子应用devtool调试
  if (process.env.NODE_ENV !== 'production' && window.__POWERED_BY_QIANKUN__) {
    let instanceDiv = document.createElement('div')
    instanceDiv.id = 'customer-service-system-app-debug'
    instanceDiv.__vue__ = instance
    document.body.appendChild(instanceDiv)
  }
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  console.log('独立运行')
  render()
}

export async function bootstrap(props) {}

export async function mount(props) {
  console.log('[qiankun-mount]-智能客服系统-mount')
  const { mainStore, mainRoute } = props

  Vue.prototype.$mainStore = mainStore
  Vue.prototype.$mainRoute = mainRoute
  Vue.prototype.$showNavBar = props.subSystemNavbarStatus.showNavBar

  // 单入口 可能需要注释掉下面的 onGlobalStateChange
  Vue.prototype.$onGlobalStateChange = props.onGlobalStateChange
  Vue.prototype.$onGlobalStateChange((state) => {
    store.commit('global/setGlobalState', { ...state })
  })

  common.initGlobalState(store, props)

  render(props)
}

export async function unmount() {
  console.log('[qiankun-智能客服系统-unmount] ')
  const cachedInstance = instance.cachedInstance || instance
  window.__CACHE_INSTANCE_BY_QIAN_KUN_FOR_VUE__ = cachedInstance
  const cachedNode = cachedInstance._vnode
  if (!cachedNode.data.keepAlive) cachedNode.data.keepAlive = true
  cachedInstance.catchRoute = {
    apps: [...instance.$router.apps]
  }
  instance.$destroy()
  router = null
  instance.$router.apps = []
}
